package com.travel.server.manager.impl.dal;

import com.travel.server.manager.dao.OrderTravelInfoDao;
import com.travel.server.manager.model.OrderTravelInfo;
import com.travel.server.manager.model.OrderTravelInfoExample;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @Author: LiZhenhui
 * @Date: 2018/11/5 18:26
 * @Desc:
 */
@Component
public class TravelDal {

    @Autowired
    private OrderTravelInfoDao orderTravelInfoDao;

    public OrderTravelInfo selectById(Integer id){
        return orderTravelInfoDao.selectByPrimaryKey(id);
    }

    public PageDto<OrderTravelInfo> select(TravelParam param){
        OrderTravelInfoExample example = makeExample(param);
        int total = orderTravelInfoDao.countByExample(example);
        example.setOrderByClause("update_time desc");
        List<OrderTravelInfo> list = orderTravelInfoDao.selectByExample(example);

        PageDto<OrderTravelInfo> pageDto = new PageDto<>();
        pageDto.setTotal(total);
        pageDto.setList(list);

        return pageDto;
    }

    public OrderTravelInfoExample makeExample(TravelParam param){
        OrderTravelInfoExample example = new OrderTravelInfoExample();
        OrderTravelInfoExample.Criteria criteria = example.or();
        if(param.getOrderId() != null){
            criteria.andOrderIdEqualTo(param.getOrderId());
        }
        if(param.getOffset() != null && param.getLimit() != null){
            example.setOffset(param.getOffset());
            example.setLimit(param.getLimit());
        }
        return example;
    }

    @Data
    public static class TravelParam{

        private Integer orderId;

        private Integer offset;

        private Integer limit;
    }
}
